
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Leon
 */
public class Game extends GameCanvas implements Runnable {

    //Khai bao bien
    int x = -1;
    int y = -1;
    int soVit;
    private Graphics G;
    public boolean Running;
    public long Gameticks = 0;
    public boolean isGameover = false;
    public boolean isWinner = false;
    public boolean started = false;

    //An vao man hinh tao song duoi vit
    protected void pointerPressed(int x, int y) {
        super.pointerPressed(x, y);
        try {
            GameManager.CreateWaves(x, y);
            this.x = x;
            this.y = y;

        } catch (Exception error) {
        }
    }

    public Game(int soVit) throws IOException {
        super(true);
        this.soVit = soVit;

        if (started == false) {
            G = getGraphics();
            Image img = Image.createImage("/resource/logo.PNG");
            this.G.drawImage(img, 45, 50, 0);
        }

    }

    public void Start(int speed) throws IOException {
        GameManager.StartWith(this);
        for (int i = 0; i < soVit; i++) {
            GameManager.CreateDuck(GameManager.GetRandom(20, 220), GameManager.GetRandom(20, 280), speed);
        }
        Running = true;
        (new Thread(this)).start();
    }

    public void run() {
        while (Running) {
            try {
                GameManager.Update(G, x, y);
                x = -1;
                y = -1;
                Gameticks++;
                if (GameManager.soVit() <= 1) {
                    isGameover = true;
                }
                if (isWinner) {
                    Running = false;
                }

            } catch (IOException ex) {
            }
            try {
                Thread.sleep(20);
            } catch (InterruptedException ex) {
            }
        }
    }
}
